package com.ruoyi.huanghefd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.huanghefd.domain.ApplysheetDev;
import com.ruoyi.huanghefd.domain.ApplysheetDev1;
import com.ruoyi.huanghefd.domain.Makesheet;
import com.ruoyi.huanghefd.domain.MakesheetDetails;
import com.ruoyi.huanghefd.mapper.ApplysheetDev1Mapper;
import com.ruoyi.huanghefd.service.ApplysheetDevService;
import com.ruoyi.huanghefd.mapper.ApplysheetDevMapper;
import com.ruoyi.huanghefd.utils.PageBean;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 *
 */
@Service
public class ApplysheetDevServiceImpl extends ServiceImpl<ApplysheetDevMapper, ApplysheetDev>
    implements ApplysheetDevService{
    private static final Logger log = LoggerFactory.getLogger(ApplysheetDevServiceImpl.class);

    @Resource
    ApplysheetDevMapper applysheetDevMapper;

    @Resource
    ApplysheetDev1Mapper applysheetDev1Mapper;

    @Override
    public AjaxResult addApplysheetDev(ApplysheetDev applysheetDev) {
        int result = 0;
        try {
            //申请单*（检修设备试运行、恢复运行） 主键
            String strApId = UUID.randomUUID().toString().replace("-", "");
            //申请单*（检修设备试运行、恢复运行） 票号
            String strApCode = this.getGenerateOrder(applysheetDev.getApType());
            log.info("生成票号："+strApCode);
            applysheetDev.setApCode(strApCode);
            applysheetDev.setApId(strApId);

            //插入操作票主表
            log.info("新增（申请单检修设备试运行、恢复运行）-主表开始>>" + applysheetDev);
            result = applysheetDevMapper.insertAll(applysheetDev);
            log.info("新增（申请单检修设备试运行、恢复运行）-主表结束<<" + result);

            //插入操作票明细表
            if(!CollectionUtils.isEmpty(applysheetDev.getDev1())) {

                List<ApplysheetDev1> listmsdtl=new ArrayList();

                for (ApplysheetDev1 msdtls : applysheetDev.getDev1()
                ) {
                    String uuid = UUID.randomUUID().toString().replace("-", "");
                    log.info("uuid="+uuid);

                    ApplysheetDev1 msdtl=new ApplysheetDev1();
                    msdtl.setApId(uuid);
                    msdtl.setApCode(strApCode);
                    msdtl.setApType(msdtls.getApType());
                    msdtl.setApOrder(msdtls.getApOrder());
                    msdtl.setSafetyMeasure(msdtls.getSafetyMeasure());
                    msdtl.setApResult(msdtls.getApResult());

                    log.info("msdtl="+msdtl);
                    listmsdtl.add(msdtl);
                }
                log.info("新增（申请单检修设备试运行、恢复运行）-明细表开始>>" + listmsdtl);
                result = applysheetDev1Mapper.insertBatch(listmsdtl);
                log.info("新增（申请单检修设备试运行、恢复运行）-明细表结束<<" + result);
            }
            return AjaxResult.success(strApCode);
        } catch (Exception e) {
            return AjaxResult.error("检修设备试运行、恢复运行申请单新增失败", e);
        }
    }

    @Override
    public AjaxResult delApplysheetDev(String strApCode) {
        int result = 0;
        try {
            log.info("删除检修设备试运行、恢复运行申请单-主表开始>>");
            result = applysheetDevMapper.delByApCode(strApCode);
            log.info("删除检修设备试运行、恢复运行申请单-主表结束<<"+result);

            log.info("删除检修设备试运行、恢复运行申请单-从表开始>>");
            result = applysheetDev1Mapper.delByApCode(strApCode);
            log.info("删除检修设备试运行、恢复运行申请单-从表结束<<"+result);

            return AjaxResult.success(strApCode);
        } catch (Exception e) {
            return AjaxResult.error("检修设备试运行、恢复运行申请单删除失败", e);
        }
    }

    @Override
    public AjaxResult uptApplysheetDev(ApplysheetDev applysheetDev) {
        int result = 0;
        try {
            log.info("修改检修设备试运行、恢复运行申请单-主表开始>>");
            result = applysheetDevMapper.updatebyApId(applysheetDev);
            log.info("修改检修设备试运行、恢复运行申请单-主表结束<<"+result);

            if(!CollectionUtils.isEmpty(applysheetDev.getDev1())){
                log.info("修改检修设备试运行、恢复运行申请单-从表开始>>");
                //result = applysheetDev1Mapper.updateBatchByApId(applysheetDev.getApdls());
                //最好的方式还是先删后插
                applysheetDev1Mapper.delByApCode(applysheetDev.getApCode());
                applysheetDev1Mapper.insertBatch(applysheetDev.getDev1());

                log.info("修改检修设备试运行、恢复运行申请单-从表结束<<"+result);
            }
            return AjaxResult.success(applysheetDev.getApCode());
        } catch (Exception e) {
            return AjaxResult.error("检修设备试运行、恢复运行申请单修改失败", e);
        }
    }

    @Override
    public AjaxResult queryApplysheetDevs(String strApCode, String strApDept, String strApState, String strUserId, String strMailType) {
        try {
            List<ApplysheetDev> applysheetDevList = applysheetDevMapper.queryApplysheetDevList(strApCode, strApDept,strApState,strUserId,strMailType);
            if (CollectionUtils.isEmpty(applysheetDevList)) {
                return AjaxResult.success("检修设备试运行、恢复运行申请单查询无数据",null);
            }
            return AjaxResult.success("检修设备试运行、恢复运行申请单查询成功",applysheetDevList);
        } catch (Exception e) {
            return AjaxResult.warn("操作票查询失败",e);
        }
    }

    @Override
    public PageBean queryApplysheetDevsByPage(String strApCode, String strApDept, String strApState, String strUserId, String strMailType, int pageNum, int pageSize) {
        PageBean pageBean = new PageBean();
        if (pageNum > 0 && pageSize > 0) {
            int totalRecord = applysheetDevMapper.getApplysheetDevNum(strApCode, strApDept, strApState, strUserId, strMailType);
            pageBean = new PageBean(pageNum, pageSize, totalRecord);

            //从该行开始拿数据
            int startIndex = (pageNum - 1) * pageSize;
            pageBean.setStartIndex(startIndex);
            pageBean.setCode(200);

            //数据集合
            List<ApplysheetDev> applysheetDevList = applysheetDevMapper.queryApplysheetDevListByPage(strApCode, strApDept, strApState, strUserId, strMailType,startIndex, pageSize);
            pageBean.setList(applysheetDevList);
            pageBean.setSuccessMsg("检修设备试运行、恢复运行申请单查询成功！");
        } else {
            pageBean.setCode(500);
            pageBean.setFailMsg("检修设备试运行、恢复运行申请单查询失败！");
        }
        return pageBean;
    }

    /**
     * @Author gnplum
     * @Description //操作票票号
     * @Date 14:56 2023/11/12
     * @param Type
     * @return java.lang.String
     **/
    private String getGenerateOrder(String Type) {

        log.info("获取票编号=" + Type);
        String strWsCode = "";
        int order = 0;
        try {

            order = applysheetDevMapper.getApCodeNum(Type);
            log.info("order=" + order);
            //DQ1202311110001
            strWsCode = Type + new SimpleDateFormat("yyyyMMdd").format(new Date()) + String.format("%03d", order);

            log.info("strWsCode=" + strWsCode);

        } catch (Exception e) {
            strWsCode = "";
            //e.printStackTrace();
            //throw  e;
            //log.error(e.getCause()+e.getMessage());
        }
        return strWsCode;
    }
}




